<?php

namespace app\advertisement\logic;

use app\api\logic\BasicLogic;
use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;
use think\cache\driver\Redis;

class AdvplatformLogic extends BasicLogic
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new \app\advertisement\model\Advplatform();
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $limit = $params['limit'] ?? 10;
        $title = $params['title'] ?? "";
        $status = $params['status'] ?? "";

        $where = [];
        if (!empty($title)) {
            $where[] = ['title', 'like', '%' . $title . '%'];
        }
        if (!empty($status)) {
            $where[] = ['status', '=', $status];
        }

        $list = $this->model
            ->where($where)
            ->paginate($limit);
        $lists = $list->items();
        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $lists = [];
        $header = [];
        // $header[] = ['广告账户', 'advertiser_id'];
        $fileName = "export_Advplatform" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }

    public function add($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $result = false;
        $result = Db::name('advplatform')->insert($data);
        if ($result !== false) {
            return json(DefaultResponse(200, true, '添加成功!', null));
        } else {
            return json(DefaultResponse(201, false, '添加失败!', null));
        }
    }

    public function find($id)
    {
        $row = $this->model->find($id);
        if (empty($row)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        return json(DefaultResponse(200, true, '获取成功', $row));
    }

    public function edit($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $id = $data['id'];
        $row = $this->model->find($id);
        if (empty($row)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        $result = false;

        try {
            $result = $row->save($data);
            if ($result) {
                return json(DefaultResponse(201, false, '更新成功!', null));
            }
            return json(DefaultResponse(201, false, '更新失败!', null));
        } catch (ValidateException $e) {
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            return json(DefaultResponse(201, false, '更新失败!', null));
        }
        return json(DefaultResponse(201, false, '更新成功!', null));
    }


    public function delete($data = [])
    {
        $id = $data['id']??'';
        if(!$id) {
            return $this->jsonMessage(201,false,'id不能为空',null);
        }
        $this->model->where(['id'=>$id])->delete();
        return      $this->jsonMessage(200,true,'操作成功!',null);
    }
    
}
